package com.xuebusi.mina.client.controller;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.InetSocketAddress;

@RestController
public class MinaController {

    @Autowired
    private InetSocketAddress socketAddress;

    @Autowired
    private NioSocketConnector connector;

    @GetMapping
    public String sendMsg(@RequestParam("msg") String msg) {
        // 建立连接
        ConnectFuture cf = connector.connect(socketAddress);
        // 等待连接创建完成
        cf.awaitUninterruptibly();
        // 发送消息，中英文符号都有
        cf.getSession().write(msg);
        return "success" + System.currentTimeMillis() + ":" + msg;
    }
}
